/***************************************************************************
 创建者: 华磊
 开始时间: 2018.8.14
 copyright: (C) 华友高科
 修改说明: (每次有修改就添加一条，带有 修改人，修改时间，修改描述)
 example (1) hualei 2017.5.8 类的具体实现代码编写

 ***************************************************************************/
#include "modbusiomoduleconfig.h"
#include <qdebug.h>
#include <qsettings.h>

ModbusIoModuleConfig::ModbusIoModuleConfig(QString fileName)
{
    isInitialOk=false;
    if(1!=readConfigFile(fileName))
    {
        isInitialOk=false;
    }
    else
    {
        isInitialOk=true;
    }
}



int ModbusIoModuleConfig::readConfigFile(QString fileName)
{
    if(1!=readCommunicateParameter(fileName))
    {
        return -1;
    }

    if(1!=readRegAddressParameter(fileName))
    {
        return -2;
    }

    if(1!=readVendorId(fileName))
    {
        return -3;
    }
    if(1!=readCount(fileName))
    {
        return -4;
    }


    qDebug() << "ModbusIoModuleConfig::readConfigFile sucess";
    return 1;

}

bool ModbusIoModuleConfig::isInitialOkStatus()
{
    return isInitialOk;
}

int ModbusIoModuleConfig::getParameter(CommunicateParameter &comParameterReturn,IoModuleRegAddressInfo &regInfoReturn
                                       , E_VENDOR_ID &vendorIdReturn)
{
    if(true==isInitialOk)
    {
        regInfoReturn=regAddInfo;
        comParameterReturn=comParameter;
        vendorIdReturn=vendorId;
        return 1;
    }
    else
    {
        return -1;
    }
}


int ModbusIoModuleConfig::getCommunicateParameter(CommunicateParameter& comParameterReturn)
{
    if(true==isInitialOk)
    {
        comParameterReturn=comParameter;
        return 1;
    }
    else
    {
        return -1;
    }

}



int ModbusIoModuleConfig::getRegAddressParameter(IoModuleRegAddressInfo& regAddReturn)
{
    if(true==isInitialOk)
    {
        regAddReturn=regAddInfo;
        return 1;
    }
    else
    {
        return -1;
    }
}

int ModbusIoModuleConfig::getVendorId(int& vendorIdReturn)
{
    if(true==isInitialOk)
    {
        vendorIdReturn=vendorId;
        return 1;
    }
    else
    {
        return -1;
    }
}

int ModbusIoModuleConfig::getDiDoCount(int& diCounter,int& doCounter,int& aiCounter, int& aoCounter)
{
    if(true==isInitialOk)
    {
        diCounter=diCount;
        doCounter=doCount;
        aiCounter=aiCount;
        aoCounter=aoCount;
        return 1;
    }
    else
    {
        return -1;
    }
}



int ModbusIoModuleConfig::readCommunicateParameter(QString fileName)
{
    qDebug()<<"ModbusIoModuleConfig::readCommunicateParameter fileName:"<<fileName;
    QVariant strConfigValue;
    QString firstTimeFlagStr = fileName;
    QString strConfigGroup = "";
    QString strConfigKey = "";

    QSettings settings(firstTimeFlagStr,QSettings::IniFormat);
    settings.setIniCodec("UTF-8");
    strConfigGroup = "CommunicateInfo";
    settings.beginGroup(strConfigGroup);

    //modbusType
    strConfigKey = "modbusType";
    if(false==settings.contains(strConfigKey))
    {
        return -1;
    }
    strConfigValue = settings.value(strConfigKey);
    comParameter.modbusType = (E_MODBUS_TYPE)strConfigValue.toInt();

    //ip
    strConfigKey = "ip";
    if(false==settings.contains(strConfigKey))
    {
        return -2;
    }
    strConfigValue = settings.value(strConfigKey);
    comParameter.ip = strConfigValue.toString();

    //port
    strConfigKey = "port";
    if(false==settings.contains(strConfigKey))
    {
        return -3;
    }
    strConfigValue = settings.value(strConfigKey);
    comParameter.port = strConfigValue.toInt();


    //serialDeviceName
    strConfigKey = "serialDeviceName";
    if(false==settings.contains(strConfigKey))
    {
        return -4;
    }
    strConfigValue = settings.value(strConfigKey);
    comParameter.serialDeviceName = strConfigValue.toString();

    //parity
    strConfigKey = "parity";
    if(false==settings.contains(strConfigKey))
    {
        return -5;
    }
    strConfigValue = settings.value(strConfigKey);
    comParameter.parity = strConfigValue.toString();

    //baud
    strConfigKey = "baud";
    if(false==settings.contains(strConfigKey))
    {
        return -6;
    }
    strConfigValue = settings.value(strConfigKey);
    comParameter.baud = strConfigValue.toInt();

    //data_bit
    strConfigKey = "data_bit";
    if(false==settings.contains(strConfigKey))
    {
        return -7;
    }
    strConfigValue = settings.value(strConfigKey);
    comParameter.data_bit = strConfigValue.toInt();

    //stop_bit
    strConfigKey = "stop_bit";
    if(false==settings.contains(strConfigKey))
    {
        return -8;
    }
    strConfigValue = settings.value(strConfigKey);
    comParameter.stop_bit = strConfigValue.toInt();

    settings.endGroup();

    qDebug() << "ModbusIoModuleConfig::readCommunicateParameter sucess";
    return 1;
}


int ModbusIoModuleConfig::readVendorId(QString fileName)
{
    QVariant strConfigValue;
    QString firstTimeFlagStr = fileName;
    QString strConfigGroup = "";
    QString strConfigKey = "";

    QSettings settings(firstTimeFlagStr,QSettings::IniFormat);
    settings.setIniCodec("UTF-8");
    strConfigGroup = "GeneralInfo";
    settings.beginGroup(strConfigGroup);

    //vendorId
    strConfigKey = "vendorId";
    if(false==settings.contains(strConfigKey))
    {
        return -1;
    }
    strConfigValue = settings.value(strConfigKey);
    vendorId= (E_VENDOR_ID)strConfigValue.toInt();


    settings.endGroup();

    qDebug() << "ModbusIoModuleConfig::readVendorId sucess";
    return 1;
}


int ModbusIoModuleConfig::readCount(QString fileName)
{
    QVariant strConfigValue;
    QString firstTimeFlagStr = fileName;
    QString strConfigGroup = "";
    QString strConfigKey = "";

    QSettings settings(firstTimeFlagStr,QSettings::IniFormat);
    settings.setIniCodec("UTF-8");
    strConfigGroup = "Count";
    settings.beginGroup(strConfigGroup);

    //diCount
    strConfigKey = "diCount";
    if(false==settings.contains(strConfigKey))
    {
        return -1;
    }
    strConfigValue = settings.value(strConfigKey);
    diCount= strConfigValue.toInt();

    //doCount
    strConfigKey = "doCount";
    if(false==settings.contains(strConfigKey))
    {
        return -2;
    }
    strConfigValue = settings.value(strConfigKey);
    doCount= strConfigValue.toInt();

    //aiCount
    strConfigKey = "aiCount";
    if(false==settings.contains(strConfigKey))
    {
        return -3;
    }
    strConfigValue = settings.value(strConfigKey);
    aiCount= strConfigValue.toInt();

    //aoCount
    strConfigKey = "aoCount";
    if(false==settings.contains(strConfigKey))
    {
        return -4;
    }
    strConfigValue = settings.value(strConfigKey);
    aoCount= strConfigValue.toInt();


    settings.endGroup();

    qDebug() << "ModbusIoModuleConfig::read dido sucess";
    return 1;
}

int ModbusIoModuleConfig::readRegAddressParameter(QString fileName)
{
    QVariant strConfigValue;
    QString firstTimeFlagStr = fileName;
    QString strConfigGroup = "";
    QString strConfigKey = "";

    QSettings settings(firstTimeFlagStr,QSettings::IniFormat);
    settings.setIniCodec("UTF-8");
    strConfigGroup = "RegAddressInfo";
    settings.beginGroup(strConfigGroup);

    //diStartAdd
    strConfigKey = "diStartAdd";
    if(false==settings.contains(strConfigKey))
    {
        return -1;
    }
    strConfigValue = settings.value(strConfigKey);
    regAddInfo.diStartAdd = strConfigValue.toInt();

    //doStartAdd
    strConfigKey = "doStartAdd";
    if(false==settings.contains(strConfigKey))
    {
        return -1;
    }
    strConfigValue = settings.value(strConfigKey);
    regAddInfo.doStartAdd = strConfigValue.toInt();

    //aiStartAdd
    strConfigKey = "aiStartAdd";
    if(false==settings.contains(strConfigKey))
    {
        return -1;
    }
    strConfigValue = settings.value(strConfigKey);
    regAddInfo.aiStartAdd = strConfigValue.toInt();

    //aoStartAdd
    strConfigKey = "aoStartAdd";
    if(false==settings.contains(strConfigKey))
    {
        return -1;
    }
    strConfigValue = settings.value(strConfigKey);
    regAddInfo.aoStartAdd = strConfigValue.toInt();


    settings.endGroup();

    qDebug() << "ModbusIoModuleConfig::readRegAddressParameter sucess";
    return 1;

}



